import fitz
import os
import openpyxl
def getpages(flfp):
    doc = fitz.open(flfp)
    return doc.page_count
if __name__=="__main__":
    str1= input("1.请输入文件夹")
    if os.path.isdir(str1) == False:
        exit(0)
    li=[]
    for root, dirs, files in os.walk(str1):
        for name in files:
            if os.path.splitext(name)[-1]==".pdf":
                print("pdf",name)
                flfp = os.path.join(root, name)
                str2 = flfp[len(str1):]
                str3="A4"
                if flfp.find("A4")>0:
                    str3 ="A4"
                elif flfp.find("A3")>0:
                    str3="A3"
                li.append([flfp,str2,getpages(flfp),str3])
    print(li)
    wb = openpyxl.Workbook()
    # 获取当前活跃的worksheet,默认就是第一个worksheet
    ws = wb.active
    ws.cell(row=1, column=1).value = "PDF路径"
    ws.cell(row=1, column=2).value = "相对路径"
    ws.cell(row=1, column=3).value = "页数"
    ws.cell(row=1, column=4).value = "纸张"


    for i, a  in enumerate(li):
        ws.cell(row=i + 2, column=1).value = a[0]
        ws.cell(row=i + 2, column=2).value = a[1]
        ws.cell(row=i + 2, column=3).value = a[2]
        ws.cell(row=i + 2, column=4).value = a[3]
    wb.save(filename= os.path.join(str1,"text.xlsx"))
    wb.close()
    str1 = input("Any key to exit!")